package com.ac.controller;

import com.ac.common.ServerResponse;
import com.ac.pojo.User;
import com.ac.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService iUserService;

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * 查询数据库所有用户控制器
     * @return
     */
    @RequestMapping(value = "selectAll.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<List<User>> selectAll(){
        ServerResponse<List<User>> response = iUserService.selectAll();
        logger.info("输入日志");
        return response;
    }

    /**
     * 查询单个用户
     * @param username
     * @return
     */
    @RequestMapping(value = "selectSingleUser.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> selectSingleUser(String username){
        ServerResponse<User> response = iUserService.selectSingleUser(username);
        return response;
    }

    /**
     * 添加用户
     * @param username
     * @param password
     * @param email
     * @return
     */
    @RequestMapping(value = "addUser.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addUser(String username,String password,String email){
        User user = new User(username,password,email);
        ServerResponse response = iUserService.addUser(user);
        return response;
    }

    /**
     * 删除用户,核对用户名与密码以及邮箱是否正确
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "deleteUser.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse deleteUser(String username,String password,String email){
        ServerResponse response = iUserService.deleteUser(username,password,email);
        return response;
    }

    /**
     * 根据用户名更新用户
     * @param username
     * @param password
     * @param email
     * @return
     */
    @RequestMapping(value = "updateUserInfo.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> updateUserInfo(String username,String password,String email){
        User user = new User(username,password,email);
        ServerResponse<User> response = iUserService.updateUserInfo(user);
        return response;
    }

    /**
     * 分页后台
     * @param page 默认为1
     * @param limit 默认为5
     * @return
     */
    @RequestMapping(value = "list.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse list(@RequestParam(value = "page",defaultValue = "1") int page,
                                     @RequestParam(value = "limit",defaultValue = "5") int limit ){
        ServerResponse response = iUserService.getUserList(page,limit);
        return response;
    }
}
